package com.platform.utils;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

public class PicUtils {
 
//    //缩放图片工具的构造函数
//    public PicUtils(String srcFile) throws IOException {
//        //得到最后一个.的位置
//        int index = srcFile.lastIndexOf(".");
//        //获取被缩放的图片的格式
//        this.ext = srcFile.substring(index + 1);
//        //获取目标路径(和原始图片路径相同,在文件名后添加了一个_s)
//        this.destFile = srcFile.substring(0, index) + "_s." + ext;
//        //读取图片,返回一个BufferedImage对象
//        //获取图片的长和宽
//    }
 
    /**
     * 按比例对图片进行缩放.
     * @param scale 缩放比例
     * @throws IOException
     */
    public void zoomByScale(double scale,String srcFile,String destFile) throws IOException {
        OutputStream out = new FileOutputStream(destFile);
        InputStream in = new FileInputStream(srcFile);
        int index = srcFile.lastIndexOf(".");
        String ext = srcFile.substring(index + 1);
    	zoomByScale(scale,in,out,ext);
    }
    
    /**
     * 按比例对图片进行缩放.
     * @param scale 缩放比例
     * @throws IOException
     */
    public static void zoomByScale(double scale,InputStream in,OutputStream out,String ext) throws IOException {
    	BufferedImage img = ImageIO.read(in);
        int width = img.getWidth();
        int height = img.getHeight();
        //获取缩放后的长和宽
        int _width = (int) (scale * width);
        int _height = (int) (scale * height);
        //获取缩放后的Image对象
        Image _img = img.getScaledInstance(_width, _height, Image.SCALE_DEFAULT);
        //新建一个和Image对象相同大小的画布
        BufferedImage image = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB);
        //获取画笔
        Graphics2D graphics = image.createGraphics();
        //将Image对象画在画布上,最后一个参数,ImageObserver:接收有关 Image 信息通知的异步更新接口,没用到直接传空
        graphics.drawImage(_img, 0, 0, null);
        //释放资源
        graphics.dispose();
        //使用ImageIO的方法进行输出,记得关闭资源
        ImageIO.write(image, ext, out);
        out.close();
    }
    public static void zoomBySize(int width, int height,String srcFile,String destFile) throws IOException {
        OutputStream out = new FileOutputStream(destFile);
        InputStream in = new FileInputStream(srcFile);
        int index = srcFile.lastIndexOf(".");
        String ext = srcFile.substring(index + 1);
        zoomBySize(width,height,in,out,ext);
    } 
    /**
     * 指定长和宽对图片进行缩放
     * @param width 长
     * @param height 宽
     * @throws IOException
     */
    public static void zoomBySize(int width, int height,InputStream in,OutputStream out,String ext) throws IOException {
        //与按比例缩放的不同只在于,不需要获取新的长和宽,其余相同.
    	BufferedImage img = ImageIO.read(in);

        Image _img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();
        graphics.drawImage(_img, 0, 0, null);
        graphics.dispose();
        ImageIO.write(image, ext, out);
        out.close();
    }
    
	
	/**
	 * 剔除周边边框
	 * @param fn
	 * @throws IOException
	 */
	public static void delFrame(InputStream in,OutputStream out) throws IOException{
		BufferedImage src=ImageIO.read(in);
		int x1,y1,x2,y2;//左上角，右下角
		int w=src.getWidth();
		int h=src.getHeight();
		x1=w;y1=h;x2=0;y2=0;
		for(int i=0;i<w;i++){
			for(int j=0;j<h;j++){
				int rgb=src.getRGB(i, j);
				if(rgb!=0xffffff){//不透明
					if(i<x1) x1=i;
					if(j<y1) y1=j;
					if(i>x2) x2=i;
					if(j>y2) y2=j;
				}
			}
		}
		BufferedImage outImg=src.getSubimage(x1, y1, x2-x1+1, y2-y1+1);
		ImageIO.write(outImg, "png", out);
	}
	
	public static void main(String[] args) {
        try {
			OutputStream out = new FileOutputStream("f:\\larker_img\\aim2.png");
			InputStream in = new FileInputStream("f:\\larker_img\\aim.png");
			delFrame(in,out);
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
